const multer = require('multer')
const path = require('path')

const storage = multer.diskStorage({
    destination(req, file, callback) {
        console.log(req.baseUrl)
        console.log(file.fieldname)
        callback(null, path.resolve(__dirname, '../public/resources/imgs'))
    },
    filename(req, file, callback) {
        console.log(req.url)
        let originalname = file.originalname
        let lastIndex = originalname.lastIndexOf('.') + 1
        let suffix = originalname.slice(lastIndex) || file.mimetype.split('/')[1]
        let dt = new Date()
        let yy = dt.getFullYear()
        let mm = dt.getMonth() + 1
        let dd = dt.getDate();
        mm = mm < 10 ? '0' + mm : mm
        dd = dd < 10 ? '0' + dd : dd
        let str = `${yy}${mm}${dd}`
        let filename = file.fieldname + '-' + str + '-' + Date.now() + '.' + suffix;
        callback(null, filename);
    }
});

const upload = multer({ storage: storage });

module.exports = upload